package cn.telling.product.dao.Impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import cn.telling.common.util.TellingConstance;
import cn.telling.product.dao.INoDirectionalDao;
import cn.telling.utils.BaseUtil;
import cn.telling.utils.StringHelperTools;

/**
 * 
 * @ClassName: NoDirectionalDaoImpl 
 * @Description: 产品非定向发布数据服务接口的实现
 * @author 陈赟 
 * @date 2015年6月18日 上午11:24:11 
 *
 */
@Repository("noDirectionalDao")
public class NoDirectionalDaoImpl implements INoDirectionalDao {
    
	private static final Logger LOGGER = LoggerFactory.getLogger(NoDirectionalDaoImpl.class); 
	
	@Autowired
	@Qualifier("film-template")
	private JdbcTemplate jdbcTemplate;
	
	/**保存非定向数据*/
	@Override
	public int insertNoDirectional(String shopProductId,String priceretailonline,String shareCode, String stockNumber,String areaId,String userId,String isShare, String gifts, String isContainInvoice,String pkStore) {
		int supplyArea0Id = 0;
		try{
			String sql = "update shop_product set isfeng = ? where id = ? ";
			Object args [] = new Object[]{ TellingConstance.NOFENG,shopProductId };
			args = StringHelperTools.nullToString(args);
			LOGGER.debug("更新shop_product为非定向发布模式:"+BaseUtil.logSQL(sql,args));
			
			//删除supply_area0数据
			String sqldel = "update supply_area0 set del_flag = ?, update_user = ? where supplyproductid = ? and saletype in ( '" + TellingConstance.SALETYPE3 + "','" + TellingConstance.SALETYPE4 + "') ";
			Object argsdel [] = new Object[]{ TellingConstance.DEL_FLAG_0, userId, shopProductId };
			argsdel = StringHelperTools.nullToString(argsdel);
			LOGGER.debug("删除supply_area0数据:"+BaseUtil.logSQL(sqldel,argsdel));
			
			jdbcTemplate.update(sqldel, argsdel);
			
			//更新supply_area0表里面的数据
			String sqlSEQ ="select SEQ_SUPPLY_AREA0_PK.NEXTVAL from dual ";
			List<String> pkList = (List<String>) jdbcTemplate.query(sqlSEQ,new RowMapper<String>() {
						public String mapRow(ResultSet rs, int rowNum)throws SQLException {
							return rs.getString("NEXTVAL");
						}
					});
			String supplyArea0IdStr = "";
			if(pkList != null && pkList.size() > 0){
				supplyArea0IdStr = pkList.get(0);
			}
			//组合唯一键
			String onlyID=shopProductId+"_"+areaId+"_N";
			final String sql2 =  "insert into supply_area0(id,supplyproductid,areaId,priceretailonline,stocknumber,overplusNumber,salestatus,saletype,isvalid,shareCode,isShare,del_flag,createTime,update_user,gifts,IS_CONTAIN_INVOICE,status,onshelftime,PK_STORDOC,ONLYID) values ('"+supplyArea0IdStr+"',?,?,?,?,?,?,?,?,?,?,?,sysdate,?,?,?,?,sysdate,?,?) ";
			Object args2 [] = new Object[]{shopProductId,areaId,
					priceretailonline,stockNumber,stockNumber,
					TellingConstance.SALESTATUS3,TellingConstance.SALETYPE2,
					TellingConstance.IS_VALID_01,shareCode,isShare,
					TellingConstance.DEL_FLAG_1,
					userId,gifts,isContainInvoice,TellingConstance.STATUS_1,pkStore,
					onlyID
				};
			args2 = StringHelperTools.nullToString(args2);

			LOGGER.debug("更新supply_area0表的数据:"+BaseUtil.logSQL(sql2,args2));
			jdbcTemplate.update(sql, args);
			jdbcTemplate.update(sql2,args2);
			supplyArea0Id = Integer.valueOf(supplyArea0IdStr);
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return supplyArea0Id;
	}
	/**
	 * 检查发布的数据是否已经存在
	 * @param shopProductId
	 * @param areaId
	 * @return
	 */
	public boolean checkSupplyArea0(String shopProductId,String areaId){
		StringBuffer sb = new StringBuffer();
		sb.append(" select count(1) from supply_area0 sa ");
		sb.append(" where sa.supplyproductid=? and sa.areaid=? and sa.del_flag=?");
		Object[] args= new Object[]{
				shopProductId,
				areaId,
				TellingConstance.DEL_FLAG_1
		};
		LOGGER.debug("查询supply_area0表是否有这样的数据:"+BaseUtil.logSQL(sb.toString(),args));
		boolean flag=jdbcTemplate.queryForInt(sb.toString(), args)>0?true:false;
		return flag;
	}
	
}